/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.zx.credit.rule.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.zx.credit.rule.entity.TcreditRule;
import com.jeesite.modules.zx.credit.rule.service.TcreditRuleService;

/**
 * 学分规则Controller
 * @author zpp
 * @version 2020-02-13
 */
@Controller
@RequestMapping(value = "${adminPath}/zx/credit/rule/tcreditRule")
public class TcreditRuleController extends BaseController {

	@Autowired
	private TcreditRuleService tcreditRuleService;
	
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public TcreditRule get(String id, boolean isNewRecord) {
		return tcreditRuleService.get(id, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("zx:credit:rule:tcreditRule:view")
	@RequestMapping(value = {"list", ""})
	public String list(TcreditRule tcreditRule, Model model) {
		model.addAttribute("tcreditRule", tcreditRule);
		return "modules/zx/credit/rule/tcreditRuleList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("zx:credit:rule:tcreditRule:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<TcreditRule> listData(TcreditRule tcreditRule, HttpServletRequest request, HttpServletResponse response) {
		tcreditRule.setPage(new Page<>(request, response));
		Page<TcreditRule> page = tcreditRuleService.findPage(tcreditRule);
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("zx:credit:rule:tcreditRule:view")
	@RequestMapping(value = "form")
	public String form(TcreditRule tcreditRule, Model model) {
		model.addAttribute("tcreditRule", tcreditRule);
		return "modules/zx/credit/rule/tcreditRuleForm";
	}

	/**
	 * 保存学分规则
	 */
	@RequiresPermissions("zx:credit:rule:tcreditRule:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated TcreditRule tcreditRule) {
		tcreditRuleService.save(tcreditRule);
		return renderResult(Global.TRUE, text("保存学分规则成功！"));
	}
	
	/**
	 * 停用学分规则
	 */
	@RequiresPermissions("zx:credit:rule:tcreditRule:edit")
	@RequestMapping(value = "disable")
	@ResponseBody
	public String disable(TcreditRule tcreditRule) {
		tcreditRule.setStatus(TcreditRule.STATUS_DISABLE);
		tcreditRuleService.updateStatus(tcreditRule);
		return renderResult(Global.TRUE, text("停用学分规则成功"));
	}
	
	/**
	 * 启用学分规则
	 */
	@RequiresPermissions("zx:credit:rule:tcreditRule:edit")
	@RequestMapping(value = "enable")
	@ResponseBody
	public String enable(TcreditRule tcreditRule) {
		tcreditRule.setStatus(TcreditRule.STATUS_NORMAL);
		tcreditRuleService.updateStatus(tcreditRule);
		return renderResult(Global.TRUE, text("启用学分规则成功"));
	}
	
	/**
	 * 删除学分规则
	 */
	@RequiresPermissions("zx:credit:rule:tcreditRule:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(TcreditRule tcreditRule) {
		tcreditRuleService.delete(tcreditRule);
		return renderResult(Global.TRUE, text("删除学分规则成功！"));
	}
	
}